package ca.etsmtl.log660.view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ca.etsmtl.log660.controller.ControllerFacade;
import ca.etsmtl.log660.security.SecurityUtils;

class LoginFrame extends JDialog implements ActionListener {

	private static final long serialVersionUID			= 3923452211220353579L;
	private static final Dimension FRAME_SIZE			= new Dimension(350, 175);
	private static final String FRAME_TITLE				= "Connexion";
	private static final String LBL_EMAIL				= "Courriel :";
	private static final String LBL_PASSWORD			= "Mot de passe :";
	private static final String LBL_LOGIN				= "Connexion";
	private static final String LBL_CANCEL				= "Annuler";
	private static final String LBL_LOGIN_ERROR			= "Les informations de connexions saisies sont incorrectes";
	private static final String LOGIN_ERROR_TITLE		= "Error lors de la connexion";
	
	private static final String DEFAULT_USER_EMAIL		= "RobertCFlores21@gmail.com";
	private static final String DEFAULT_USER_PASSWORD	= "eishie3meiH";
	
	private MainFrame parent;
	private JPanel contentPanel;
	private JPanel buttonPanel;
	private JButton btLogin;
	private JButton btCancel;
	private JLabel lblEmail;
	private JLabel lblPassword;
	private JTextField txtEmail;
	private JPasswordField txtPassword;
	
	public LoginFrame(MainFrame parentFrame) {
		this.parent = parentFrame;
		this.setModal(true);
		
		this.btLogin		= new JButton(LBL_LOGIN);
		this.btCancel		= new JButton(LBL_CANCEL);
		this.lblEmail		= new JLabel(LBL_EMAIL);
		this.lblPassword	= new JLabel(LBL_PASSWORD);
		this.txtEmail		= new JTextField();
		this.txtPassword	= new JPasswordField();
		this.contentPanel	= new JPanel(new GridLayout(6, 0));
		this.buttonPanel	= new JPanel(new GridLayout(0, 2));
		
		this.txtEmail.setText(DEFAULT_USER_EMAIL);
		this.txtPassword.setText(DEFAULT_USER_PASSWORD);
		
		this.btLogin.addActionListener(this);
		this.btCancel.addActionListener(this);
		
		this.buttonPanel.add(this.btLogin);
		this.buttonPanel.add(this.btCancel);
		this.contentPanel.add(this.lblEmail);
		this.contentPanel.add(this.txtEmail);
		this.contentPanel.add(this.lblPassword);
		this.contentPanel.add(this.txtPassword);
		this.contentPanel.add(new JPanel());
		this.contentPanel.add(this.buttonPanel);
		
		this.setContentPane(this.contentPanel);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setTitle(FRAME_TITLE);
		this.setPreferredSize(FRAME_SIZE);
		this.setSize(FRAME_SIZE);
		this.setLocationRelativeTo(null);
		this.setAlwaysOnTop(true);
		this.setVisible(true);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == this.btLogin) {
			ControllerFacade facade = new ControllerFacade();
			if (facade.logUserIn(this.txtEmail.getText(), SecurityUtils.encrypt(this.txtPassword.getText()))) {
				this.setVisible(false);
				this.parent.userLogged();
			} else {
				JOptionPane.showMessageDialog(this, LBL_LOGIN_ERROR, LOGIN_ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				this.txtPassword.setText("");
			}
		} else if (source == this.btCancel) {
			this.setVisible(false);
			this.parent.userCancelledLogin();
		}
	}

}
